package chenji.yourticket.editor;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import chenji.yourticket.Main;
import chenji.yourticket.inventory.InventoryManager.OpeningInventoryType;
import chenji.yourticket.inventory.SimpleInventory;
import chenji.yourticket.inventory.SimpleItemStack;
import chenji.yourticket.model.ModelInfo;

public class EditorInventory extends SimpleInventory {
	/**
	 * state=0 - None state=1 - DisplayNameMode state=2 - AddLoreMode state=3 -
	 * WaitRemoveLineNumber state=4 - setNode
	 */
	private int state;
	private boolean isNew;
	private ItemStack item;
	private String Name;
	private Player p;
	private ModelInfo m;
	private boolean upLoreLimited = false;

	public EditorInventory(Player p, Material material, String name) {
		super(p, (short) 7, Main.getMessageManager().getEditor_Title_First().replaceAll("%ModelName%", name), 6);
		this.isNew = true;
		this.Name = name;
		this.p = p;
		ItemStack i = new ItemStack(material);
		ItemMeta meta = i.getItemMeta();
		meta.setDisplayName("§f" +name);
		i.setItemMeta(meta);
		this.item = this.dealItem(i);
		ModelInfo m = new ModelInfo(System.currentTimeMillis(), item, name);
		this.m = m;
		this.installItemStack();
	}

	public EditorInventory(Player p, ModelInfo m) {
		super(p, (short) 7, Main.getMessageManager().getEditor_Title().replaceAll("%ModelName%", m.getName()), 6);
		this.isNew = false;
		this.item = m.getItem();
		this.Name = m.getName();
		this.p = p;
		this.m = m;
		this.installItemStack();
	}

	public void saveModelInfo() {
		m.setItem(this.item);
		Main.getPlayerDataChenter().getPlayerData(p).saveModelInfo(m);
		Main.getEditorManager().clearEditInfo(p);
	}

	private ItemStack dealItem(ItemStack itemStack) {
		ArrayList<String> lore = new ArrayList<String>();
		lore.add(Main.getConfiguration().getanti_counterfeiting().replaceAll("%player%", this.p.getName())
				.replaceAll("&", "§"));
		ItemStack s = itemStack;
		ItemMeta m = s.getItemMeta();
		m.setLore(lore);
		s.setItemMeta(m);
		return s;
	}

	public boolean isNew() {
		return this.isNew;
	}

	@Override
	public void installItemStack() {
		this.getInventory().setItem(13, this.item);
		ItemStack ia = new SimpleItemStack(Material.STAINED_GLASS_PANE,
				Main.getMessageManager().getItem_NowStyle_Lore(), Main.getMessageManager().getItem_NowStyle_Title(),
				(short) 4);

		int[] slot;
		slot = new int[] { 12, 14, 21, 22, 23 };
		for (int i : slot) {
			this.getInventory().setItem(i, ia);
		}
		ia = new SimpleItemStack(Material.STAINED_GLASS_PANE, " ", " ", (short) 7);
		slot = new int[] { 19, 20, 24, 25, 28, 29, 30, 31, 32, 33, 34, 37, 39, 41, 43 };

		for (int i : slot) {
			this.getInventory().setItem(i, ia);
		}

		ia = new SimpleItemStack(Material.ANVIL, Main.getMessageManager().getItem_EidtorInfo_Lore()
				.replaceAll("%name%", this.Name).replaceAll("%price%", this.getPrice()),
				Main.getMessageManager().getItem_EidtorInfo_Title());
		this.getInventory().setItem(10, ia);
		ia = new SimpleItemStack(Material.WOOL,
				Main.getMessageManager().getItem_Press_Lore().replaceAll("%price%", this.getPrice()),
				Main.getMessageManager().getItem_Press_Title());
		this.getInventory().setItem(11, ia);
		ia = new SimpleItemStack(Material.CHEST, " ", Main.getMessageManager().getItem_Save_Title());
		this.getInventory().setItem(15, ia);
		ia = new SimpleItemStack(Material.PAPER, Main.getMessageManager().getItem_SetDisplayName_Lore(),
				Main.getMessageManager().getItem_SetDisplayName_Title());
		this.getInventory().setItem(38, ia);

		ia = new SimpleItemStack(Material.WOOL, Main.getMessageManager().getItem_RemoveLore_Lore(),
				Main.getMessageManager().getItem_RemoveLore_Title());
		updateChengeLore();
		this.getInventory().setItem(42, ia);

		ia = new SimpleItemStack(Material.FIREBALL, Main.getMessageManager().getItem_Back_Lore(),
				Main.getMessageManager().getItem_Back_Title());
		this.getInventory().setItem(16, ia);
	}

	private void updateChengeLore() {
		ItemStack ia;
		if (this.item.getItemMeta().hasLore()) {
			int i = this.item.getItemMeta().getLore().size();

			if (i >= Main.getConfiguration().getmaxLoreLine()) {

				ia = new SimpleItemStack(Material.STAINED_GLASS_PANE,
						Main.getMessageManager().getItem_AddLoreFiled_Lore(),
						Main.getMessageManager().getItem_AddLoreFiled_Title(), (short) 14);
				this.getInventory().setItem(40, ia);
				upLoreLimited = true;
				return;
			}
		}
		ia = new SimpleItemStack(Material.ENCHANTED_BOOK, Main.getMessageManager().getItem_AddLore_Lore(),
				Main.getMessageManager().getItem_AddLore_Title());
		this.getInventory().setItem(40, ia);
		upLoreLimited = false;
	}

	private String getPrice() {
		return Double.toString(Main.getMaterialManager().getPrice(this.item.getType()));
	}

	public void InventoryClick(int slot) {

		if (slot == 38) {
			this.state = 1;
			Main.getEditorManager().registerChatListener(this.p);
			p.sendMessage(Main.getMessageManager().getMessage_Enter_DisplayName());
			p.closeInventory();

			return;
		}
		if (slot == 40) {
			if (this.upLoreLimited == true) {
				return;
			}
			this.state = 2;
			updateChengeLore();
			Main.getEditorManager().registerChatListener(this.p);
			p.sendMessage(Main.getMessageManager().getMessage_Enter_Lore());
			p.closeInventory();

			return;
		}
		if (slot == 42) {
			this.state = 3;
			Main.getEditorManager().registerChatListener(this.p);
			this.listLore();
			p.sendMessage(Main.getMessageManager().getMessage_Enter_RemoveLine());
			p.closeInventory();

			return;
		}
		if (slot == 15) {
			this.saveModelInfo();
			return;
		}
		if (slot == 11) {
			Main.getPressManager().openInventory(p, this.m);
			return;
		}
		if (slot == 16) {
			this.saveModelInfo();
			Main.getModelManager().openInventory(p);
			return;
		}

	}

	public void setChat(String message) {
		message = message.replace("&", "§");

		if (this.state == 1) {
			if (message.length() >= Main.getConfiguration().getLengthLimit()) {
				p.sendMessage(Main.getMessageManager().getMessage_Limit_length());
				this.openInventory(OpeningInventoryType.EditorManager);
				this.state = 0;
				return;
			}
			ItemMeta m = this.item.getItemMeta();
			m.setDisplayName("§f" + message);
			this.item.setItemMeta(m);
			this.openInventory(OpeningInventoryType.EditorManager);
			this.state = 0;
			this.getInventory().setItem(13, this.item);
			return;
		}
		if (this.state == 2) {
			if (message.length() >= Main.getConfiguration().getLengthLimit()) {
				p.sendMessage(Main.getMessageManager().getMessage_Limit_length());
				this.openInventory(OpeningInventoryType.EditorManager);
				this.state = 0;
				return;
			}
			ItemMeta m = this.item.getItemMeta();
			List<String> lore = m.getLore();
			lore.remove(lore.size() - 1);

			int i = lore.size();
			i--;
			if (i >= Main.getConfiguration().getmaxLoreLine()) {
				this.item = this.dealItem(this.item);
				p.sendMessage(Main.getMessageManager().getMessage_Limit_Lore());
				this.openInventory(OpeningInventoryType.EditorManager);
				return;
			}
			lore.add("§f" + message);
			lore.add(Main.getConfiguration().getanti_counterfeiting().replaceAll("%player%", this.p.getName())
					.replaceAll("&", "§"));
			m.setLore(lore);
			this.item.setItemMeta(m);
			this.openInventory(OpeningInventoryType.EditorManager);
			this.state = 0;
			this.getInventory().setItem(13, this.item);
			updateChengeLore();
			return;
		}

		if (this.state == 3) {
			ItemMeta m = this.item.getItemMeta();
			List<String> lore = m.getLore();
			int i = 0;
			try {
				i = Integer.valueOf(message);
			} catch (Exception e) {
				p.sendMessage(Main.getMessageManager().getMessage_Invalid_Number());
				this.openInventory(OpeningInventoryType.EditorManager);
				this.state = 0;
				return;
			}
			if (i < 0 || i >= lore.size()) {
				p.sendMessage(Main.getMessageManager().getMessage_Invalid_Line());
				this.openInventory(OpeningInventoryType.EditorManager);
				this.state = 0;
				return;
			}
			if (i == lore.size() - 1) {
				p.sendMessage(Main.getMessageManager().getMessage_CannotChenge());
				this.openInventory(OpeningInventoryType.EditorManager);
				this.state = 0;
				return;
			}
			lore.remove(i);
			m.setLore(lore);
			this.item.setItemMeta(m);
			this.openInventory(OpeningInventoryType.EditorManager);
			this.state = 0;
			this.getInventory().setItem(13, this.item);
			updateChengeLore();
			return;

		}

	}

	private void listLore() {
		if (this.item.getItemMeta().getLore() == null) {
			StringBuilder ss = new StringBuilder();
			ss.append("§c=====§a物品 ");
			ss.append(this.item.getItemMeta().getDisplayName());
			ss.append("§a没有叙述。§c======");
			this.p.sendMessage(ss.toString());
			return;
		}

		if (this.item.getItemMeta().getLore().isEmpty()) {
			StringBuilder ss = new StringBuilder();
			ss.append("§c=====§a物品 ");
			ss.append(this.item.getItemMeta().getDisplayName());
			ss.append("§a没有叙述。§c======");
			this.p.sendMessage(ss.toString());

		}
		StringBuilder ss = new StringBuilder();
		ss.append("§c=====§a物品 ");
		ss.append(this.item.getItemMeta().getDisplayName());
		ss.append("§a的叙述§c======");
		this.p.sendMessage(ss.toString());

		int i = 0;
		for (String line : this.item.getItemMeta().getLore()) {

			this.p.sendMessage(new StringBuilder("§c第 §a" + i + " §c行 - " + line).toString());
			i++;

		}
		return;
	}

	public boolean canClear() {
		return this.state == 0;
	}

}
